package cn.com.nwpu.visual_system.service.impl;

import cn.com.nwpu.visual_system.entity.AllSensorData;
import cn.com.nwpu.visual_system.entity.ConfigData;
import cn.com.nwpu.visual_system.entity.Sensor;
import cn.com.nwpu.visual_system.entity.SensorOperateData;
import cn.com.nwpu.visual_system.mapper.ConfigDataMapper;
import cn.com.nwpu.visual_system.mapper.SensorMapper;
import cn.com.nwpu.visual_system.mapper.SensorOperateDataMapper;
import cn.com.nwpu.visual_system.mapper.TestDataMapper;
import cn.com.nwpu.visual_system.service.SensorService;
import cn.com.nwpu.visual_system.vo.SensorOperateDataVo;
import cn.com.nwpu.visual_system.vo.ToVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @author Hutengzhong
 * @version 1.0.0
 * @title SensorServiceImpl
 * @description TODO
 * @date 2024/4/28 16:34
 */
@Service
public class SensorServiceImpl implements SensorService {
    @Autowired
    SensorOperateDataMapper sensorOperateDataMapper;
    @Autowired
    ConfigDataMapper configDataMapper;
    @Autowired
    TestDataMapper testDataMapper;

    @Override
    public HashMap<String, String[][]> getAllOperateSensor() {
        String[][][] arrays;
        // 获取当前数据的最新值
        ConfigData configData = configDataMapper.getCurrentTestDataId();
        Integer currentDataId = Integer.parseInt(configData.getConfigValue());
        List<SensorOperateData> sensorOperateDataList = sensorOperateDataMapper.getAllSensorOperateData(currentDataId + 1);

        arrays = ToVo.toOperateDataVo(sensorOperateDataList);

        HashMap<String, String[][]> resultMap = new HashMap<>();
        resultMap.put("data1", arrays[0]);
        resultMap.put("data2", arrays[1]);
        resultMap.put("data3", arrays[2]);
        resultMap.put("data4", arrays[3]);

        // 每次访问后，自动加一，模拟实时数据（用id的递增代替时间的增加）
        // 每次取10条数据
        // 交由最后负责测试的函数来更新
//        Integer count = configDataMapper.updateCurrentDataId(currentDataId % 950 + 1);
        return resultMap;
    }

    @Override
    public AllSensorData getAllSensor() {
        ConfigData configData = configDataMapper.getCurrentTestDataId();
        Integer currentDataId = Integer.parseInt(configData.getConfigValue());
        AllSensorData resultData = testDataMapper.getAllSensorData(currentDataId + 1);
        return resultData;
    }
}
